<%@page import="com.is.control.AttendanceTimeControl"%>
<%@page import="com.is.model.AttendanceTime"%>
<%@page import="com.is.util.DesData"%>
<%@page import="com.is.util.DateTime"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="com.is.util.ParamUtil"%>
<%@page import="com.is.model.AttendanceDetail"%>
<%@page import="com.is.control.AttendanceDetailControl"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.is.servlet.TokenProcessor"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="../../resources/css/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../../resources/js/jquery-1.8.1.js"></script>
<script type="text/javascript" src="../../resources/js/jquery.artDialog.js"></script>
<title>出勤记录</title>
</head>
<body>
<%

               DesData des = new DesData();
               int size = ParamUtil.getInt(request,"size" ,0);
               String token = ParamUtil.getString(request,"token","");
               System.out.print(token);
               String time = DateTime.SimpleDate();
               int courseId = ParamUtil.getInt(request,"courseId",0);
               int courseClassId = ParamUtil.getInt(request,"ccid",0);
               int stuyear = ParamUtil.getInt(request,"stuyear",0);
               if(size>0&&TokenProcessor.AllowCommited(request,token)){
            	   List<AttendanceDetail> ads = new ArrayList<AttendanceDetail>();
            	   AttendanceTime at = new AttendanceTime();
            	   at.setContent("");
            	   at.setCourseClassId(courseClassId);
            	   at.setTime(ParamUtil.getString(request, "time",DateTime.SimpleDate()));
            	   int timeId = AttendanceTimeControl.insertAttendance(at);
            	   if(timeId!=0){
	            	   for(int i=0;i<size;i++){
	            		   if(ParamUtil.getInt(request,"state"+i,5)>1){//1代表正常出勤，只需要记录未正常出勤的学生
	            		   AttendanceDetail add = new AttendanceDetail();
	            		   //add.setCourseClassId(courseClassId);
	            		   add.setReasonType(ParamUtil.getInt(request,"state"+i,5));
	            		   add.setStuId(ParamUtil.getInt(request,"student"+i,0));
	            		   add.setStuyear(stuyear);
	            		   add.setTimeId(timeId+"");
	            		   ads.add(add);
	            		   }
	            		   }
	            	   if(AttendanceDetailControl.insertAttendance(ads)){
	            		   %>
	        			   <script>
	        			   $.dialog({
							    title: '出勤记录',
							    content: '系统已将你的点名情况记录',
							    icon: 'succeed',
							    ok: function(){
	        				   			window.location.href='attendance_statistics.jsp?ccid=<%=des.encrypt(String.valueOf(courseClassId))%>';
							    }
							});
	        			   </script>
	        			   <%
	            	   }else{
	            		      AttendanceTimeControl.deleteAttendanceTimeById(timeId);
	            			   %>
	            			   <script>
	            			   $.dialog({
	              				    title: '出勤记录',
	              				    content: '抱歉，系统出现错误或您录入的信息有误，本次录入信息均无效，请重试',
	              				    icon: 'warning',
	              				    ok: function(){
	              				         history.go(-1);
	              				    }
	              				});
	            			   </script>
	            			   <%
	            	   }
            	   }else{
            		   %>
        			   <script>
        			   $.dialog({
       				    title: '出勤记录',
       				    content: '抱歉，系统出现错误或您录入的信息有误，本次录入信息均无效，请重试',
       				    icon: 'warning',
       				    ok: function(){
       				         history.go(-1);
       				    }
       				});
        			   </script>
        			   <%
            	   }
               }else{
            	   %>
    			   <script>
    			   $.dialog({
      				    title: '出勤记录',
      				    content: '请勿重复提交此次点名情况，此次提交无效',
      				    icon: 'warning',
      				    ok: function(){
      				         history.go(-1);
      				    }
      				});
    			   </script>
    			   <%
               }
%>

</body>
</html>